﻿namespace Authorization
{
    using System;

    public partial class AuthorizationServiceBase
    {
        #region UserAuthorizationServiceBase

        public OperationCollection GetOperationsForUser(string userName, string applicationName, string scopeName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref userName, true, true, false, 0, "userName");
            return UserAuthorizarionServiceBase.GetOperationsForUser(userName, applicationName, scopeName);
        }

        public string[] GetOperationNamesForUser(string userName, string applicationName, string scopeName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref userName, true, true, false, 0, "userName");
            return UserAuthorizarionServiceBase.GetOperationNamesForUser(userName, applicationName, scopeName);
        }


        public string[] GetTaskNamesForUser(string userName, string applicationName, string scopeName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref userName, true, true, false, 0, "userName");
            return UserAuthorizarionServiceBase.GetTaskNamesForUser(userName, applicationName, scopeName);
        }

        public TaskCollection GetTasksForUser(string userName, string applicationName, string scopeName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref userName, true, true, false, 0, "userName");
            return UserAuthorizarionServiceBase.GetTasksForUser(userName, applicationName, scopeName);
        }

        public RoleAssignmentCollection GetRoleAssignmentsForUser(string userName, string applicationName, string scopeName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref userName, false, false, false, 0, "userName");
            return UserAuthorizarionServiceBase.GetRoleAssignmentsForUser(userName, applicationName, scopeName);
        }

        public bool IsUserInApplicationGroup(string userName, string applicationName, string scopeName, string applicationGroupName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref applicationGroupName, true, true, false, 64, "applicationGroupName");
            SecUtility.CheckParameter(ref userName, true, true, false, 0, "userName");

            return UserAuthorizarionServiceBase.IsUserInApplicationGroup(userName, applicationName, scopeName, applicationGroupName);
        }


        public bool IsUserInRoleAssignment(string userName, string applicationName, string scopeName, string roleName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref roleName, true, true, false, 64, "roleName");
            SecUtility.CheckParameter(ref userName, true, true, false, 0, "userName");
            
            return UserAuthorizarionServiceBase.IsUserInRoleAssignment(userName, applicationName, scopeName, roleName);
        }

        public string[] GetRoleAssignmentsNamesForUser(string userName, string applicationName, string scopeName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref userName, true, true, false, 0, "userName");
            return UserAuthorizarionServiceBase.GetRoleAssignmentNamesForUser(userName, applicationName, scopeName);
        }

        public bool AccessCheck(string userName, string applicationName, int operationId, string scopeName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref userName, true, true, false, 0, "userName");
            return UserAuthorizarionServiceBase.AccessCheck(userName, applicationName, operationId, scopeName);
        }

        public bool AccessCheck(string userName, string applicationName, string operationName, string scopeName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref operationName, true, true, false, 64, "operationName");
            SecUtility.CheckParameter(ref userName, true, true, false, 0, "userName");

            return UserAuthorizarionServiceBase.AccessCheck(userName, applicationName, operationName, scopeName);
        }

        public int[] AccessCheck(string userName, string applicationName, int[] operationIds, string[] scopeNames)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref userName, true, true, false, 0, "userName");
            if (operationIds == null) throw new ArgumentNullException("operationIds");

            return UserAuthorizarionServiceBase.AccessCheck(userName, applicationName, operationIds, scopeNames);
        }

        public OperationCollection AccessCheck(string userName, string applicationName, string[] operationNames,
                                               string[] scopeNames)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckArrayParameter(ref operationNames, true, true, false, 64, "operationNames");
            SecUtility.CheckParameter(ref userName, true, true, false, 0, "userName");
            return UserAuthorizarionServiceBase.AccessCheck(userName, applicationName, operationNames, scopeNames);
        }


        public OperationCollection AccessCheck(string userName, string applicationName, string filterPattern, string[] operationNames)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckArrayParameter(ref operationNames, true, true, false, 64, "operationNames");
            SecUtility.CheckParameter(ref userName, true, true, false, 0, "userName");

            string[] scopeNames = GetAllAssignedScopesForUser(applicationName, userName, filterPattern);

            OperationCollection result = new OperationCollection();
            foreach (var scopeName in scopeNames)
            {                   
                var results =  UserAuthorizarionServiceBase.AccessCheck(userName, applicationName, operationNames, new [] { scopeName });
                               
                foreach (var operation in results)
	            {
		             if (!result.Contains(operation))
                     {
                         result.Add(operation);
                     }
            	}              

                if (result.Count == operationNames.Length)
                    break;
            }

            return result;
        }

        // TODO: CRIAR ACCESSCHECKS COM ROLE/APPLICATIONGROUP RESTRICTIONS
        // TODO:TERMINAR
        public bool AccessCheck(string userName, string applicationName, int operationId, string scopeName,
                                string[] variableNames, object[] variableValues)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref userName, true, true, false, 0, "userName");

            // TODO: TERMINAR
            // return GetUserAuthorization(applicationName).AccessCheck( operationId, scopeName,);
            return false;
        }

        #endregion

    }
}
